package elaprendiz.ventanas.buscar;

import elaprendiz.core.CriterioSQL;
import elaprendiz.core.JAbstractModelBD;
import elaprendiz.core.Paginador;
import elaprendiz.modelbd.Compra;
import elaprendiz.modelgui.ModeloTablaCompra;
import elaprendiz.util.Helper;
import java.awt.Component;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author programadorbipolar
 */
public class BuscarCompra extends javax.swing.JPanel {

    /**
     * Creates new form BuscarCompra
     */
    
    private Compra seleeccionado;
    private Paginador paginador;
    private JOptionPane op;
    private ModeloTablaCompra mtc;
    public BuscarCompra() {
        initComponents();       
         mtc = new ModeloTablaCompra();
         paginador = mtc.getPaginador();
         this.tblCompras.setModel(mtc);
         this.btnPrimero.setEnabled(false);
         this.btnAnterior.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        panelImage1 = new elaprendiz.gui.panel.PanelImage();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNroDocumento = new elaprendiz.gui.textField.TextField();
        jLabel2 = new javax.swing.JLabel();
        spItems = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        txtPrecioMayor = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        fcDespues = new com.toedter.calendar.JDateChooser();
        jLabel5 = new javax.swing.JLabel();
        fcAntes = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        txtPrecioMenor = new javax.swing.JFormattedTextField();
        btnBuscar = new elaprendiz.gui.button.ButtonRect();
        lbAviso = new elaprendiz.gui.label.LabelRect();
        jPanel2 = new javax.swing.JPanel();
        btnSeleccionar = new elaprendiz.gui.button.ButtonRect();
        btnCerrar = new elaprendiz.gui.button.ButtonRect();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        btnPrimero = new elaprendiz.gui.button.ButtonRect();
        btnAnterior = new elaprendiz.gui.button.ButtonRect();
        btnSiguiente = new elaprendiz.gui.button.ButtonRect();
        btnUltimo = new elaprendiz.gui.button.ButtonRect();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCompras = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        panelImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/elaprendiz/resources/fondoazulceleste.jpg"))); // NOI18N
        panelImage1.setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Opciones de Busqueda", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18), new java.awt.Color(255, 255, 255))); // NOI18N
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Nro de Documento:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 0);
        jPanel1.add(jLabel1, gridBagConstraints);

        txtNroDocumento.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 0);
        jPanel1.add(txtNroDocumento, gridBagConstraints);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Cantidad de Items:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 0);
        jPanel1.add(jLabel2, gridBagConstraints);

        spItems.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));
        spItems.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 3);
        jPanel1.add(spItems, gridBagConstraints);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("Precio Mayor que:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 0);
        jPanel1.add(jLabel3, gridBagConstraints);

        txtPrecioMayor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        txtPrecioMayor.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtPrecioMayor.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 0);
        jPanel1.add(txtPrecioMayor, gridBagConstraints);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setText("Despues de:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 6, 0, 0);
        jPanel1.add(jLabel4, gridBagConstraints);

        fcDespues.setDateFormatString("dd/MM/yyyy");
        fcDespues.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 6, 0, 0);
        jPanel1.add(fcDespues, gridBagConstraints);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setText("Antes de:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(3, 6, 0, 0);
        jPanel1.add(jLabel5, gridBagConstraints);

        fcAntes.setDateFormatString("dd/MM/yyyy");
        fcAntes.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 6, 0, 3);
        jPanel1.add(fcAntes, gridBagConstraints);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setText("Precio Menor que:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 0);
        jPanel1.add(jLabel6, gridBagConstraints);

        txtPrecioMenor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        txtPrecioMenor.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtPrecioMenor.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 3);
        jPanel1.add(txtPrecioMenor, gridBagConstraints);

        btnBuscar.setBackground(new java.awt.Color(51, 153, 255));
        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 6, 0);
        jPanel1.add(btnBuscar, gridBagConstraints);

        lbAviso.setBackground(new java.awt.Color(255, 0, 0));
        lbAviso.setForeground(new java.awt.Color(255, 255, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 4;
        jPanel1.add(lbAviso, gridBagConstraints);

        panelImage1.add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setOpaque(false);

        btnSeleccionar.setBackground(new java.awt.Color(51, 153, 255));
        btnSeleccionar.setText("Seleccionar");
        btnSeleccionar.setPreferredSize(new java.awt.Dimension(116, 25));
        btnSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarActionPerformed(evt);
            }
        });
        jPanel2.add(btnSeleccionar);

        btnCerrar.setBackground(new java.awt.Color(51, 153, 255));
        btnCerrar.setText("Cerrar");
        btnCerrar.setPreferredSize(new java.awt.Dimension(116, 25));
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });
        jPanel2.add(btnCerrar);

        panelImage1.add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jPanel3.setOpaque(false);
        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel4.setLayout(new java.awt.GridLayout(1, 0));

        btnPrimero.setBackground(new java.awt.Color(102, 204, 0));
        btnPrimero.setText("Primero");
        btnPrimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrimeroActionPerformed(evt);
            }
        });
        jPanel4.add(btnPrimero);

        btnAnterior.setBackground(new java.awt.Color(102, 204, 0));
        btnAnterior.setText("Anterior");
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });
        jPanel4.add(btnAnterior);

        btnSiguiente.setBackground(new java.awt.Color(102, 204, 0));
        btnSiguiente.setText("Siguiente");
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });
        jPanel4.add(btnSiguiente);

        btnUltimo.setBackground(new java.awt.Color(102, 204, 0));
        btnUltimo.setText("Ultimo");
        btnUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUltimoActionPerformed(evt);
            }
        });
        jPanel4.add(btnUltimo);

        jPanel3.add(jPanel4, java.awt.BorderLayout.PAGE_START);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(452, 250));

        tblCompras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblCompras);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        panelImage1.add(jPanel3, java.awt.BorderLayout.CENTER);

        add(panelImage1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        getOptionPane();
        op.setValue(1);
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        Paginador paginar = new Paginador();
        paginar.setLimite(3); 
        buscar(paginar); 
        this.btnAnterior.setEnabled(false);
        this.btnPrimero.setEnabled(false);
        this.btnSiguiente.setEnabled(true);
        this.btnUltimo.setEnabled(true);
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed
       paginar(paginador.siguiente());
       if(paginador.getIndexpaginaActual() == paginador.getCantidadesDePaginas())      
           this.btnUltimo.setEnabled(false);
       this.btnPrimero.setEnabled(true);
       this.btnAnterior.setEnabled(true);
    }//GEN-LAST:event_btnSiguienteActionPerformed

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
       paginar(paginador.anterior());
       this.btnUltimo.setEnabled(true);
      if(paginador.getDesplazamiento() == 0)
      {
          this.btnPrimero.setEnabled(false);
          btnAnterior.setEnabled(false);
      }
      this.btnSiguiente.setEnabled(true);
    }//GEN-LAST:event_btnAnteriorActionPerformed

    private void btnSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarActionPerformed
        if(this.tblCompras.getSelectedRow() != -1)
        {
            seleeccionado = (Compra)this.mtc.getFila(this.tblCompras.getSelectedRow());
            getOptionPane();
            op.setValue(1);
        }
        this.lbAviso.setText("Seleccione una fila de la tabla");
        
    }//GEN-LAST:event_btnSeleccionarActionPerformed

    private void btnPrimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrimeroActionPerformed
      Paginador paginar = new Paginador();
      paginar.setLimite(3); 
      buscar(paginar);
      this.btnPrimero.setEnabled(false);
      this.btnAnterior.setEnabled(false);
      this.btnSiguiente.setEnabled(true);
      this.btnUltimo.setEnabled(true);
    }//GEN-LAST:event_btnPrimeroActionPerformed

    private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUltimoActionPerformed
        Paginador paginar = new Paginador();  
        paginar.setLimite(this.paginador.getLimite()); 
        paginar.setTipoObjeto(new Compra());        
        int dezplazamiento = paginar.getCantidadesDePaginas()*paginar.getLimite(); 
        paginar.setIndexpaginaActual(paginar.getCantidadesDePaginas());
        paginar.setDesplazamiento(dezplazamiento);
        buscar(paginar);
        this.btnSiguiente.setEnabled(false);
        this.btnUltimo.setEnabled(false);
        this.btnPrimero.setEnabled(true);
        btnAnterior.setEnabled(true);
    }//GEN-LAST:event_btnUltimoActionPerformed

    private void buscar(Paginador paginar)
    {
        this.paginador = paginar;
        paginador.setTipoObjeto(new Compra());
        paginador.setLimite(3);  
        CriterioSQL criterio = paginador.getCriterio();
        criterio.setTabla(Compra.nt);
        if(!this.txtNroDocumento.getText().isEmpty())
        {
            criterio.addCondicion("numdoc", this.txtNroDocumento.getText(), "=");
        }
        
        if(this.txtPrecioMayor.getValue() != null && this.txtPrecioMenor.getValue() != null)
        {
            criterio.addCompareValorBetween("total", this.txtPrecioMayor.getValue(), this.txtPrecioMenor.getValue(), "and");
        }else
        {
            if(this.txtPrecioMayor.getValue() != null)
            {
                criterio.addCondicion("total", this.txtPrecioMayor.getValue(),">=");
            }

            if(this.txtPrecioMenor.getValue()!=null)
            {
                criterio.addCondicion("total", this.txtPrecioMenor.getValue(),"<=");
            }
            System.out.println(this.txtPrecioMenor.getValue());
        }
        
        if((Integer)this.spItems.getValue() > 0)
        {
            criterio.addCondicion("numitems", (Number)this.spItems.getValue());
        }
        
        if(this.fcAntes.getCalendar() !=null && this.fcDespues.getCalendar()!=null)
        {
            criterio.addCompareValorBetween("fecha", Helper.getFechaFormateada(this.fcDespues.getCalendar().getTime(), Helper.ANIO_MES_DIA), Helper.getFechaFormateada(this.fcAntes.getCalendar().getTime(), Helper.ANIO_MES_DIA), "and");
        }else
        {
            if(this.fcAntes.getCalendar() !=null)
            {
                criterio.addCompareTexto("fecha", Helper.getFechaFormateada(this.fcAntes.getCalendar().getTime(), Helper.ANIO_MES_DIA),"and","<=");
            }
            if(this.fcDespues.getCalendar()!=null)
            {
                criterio.addCompareTexto("fecha", Helper.getFechaFormateada(this.fcDespues.getCalendar().getTime(), Helper.ANIO_MES_DIA),"and",">=");
            }
        }
        mtc = new ModeloTablaCompra(paginador);
        this.tblCompras.setModel(mtc);
        System.out.println(criterio.getConsultaSQL());
    }
    
    
    private void paginar(List<JAbstractModelBD> regCompras)
    {
        mtc = new ModeloTablaCompra(regCompras);
        this.tblCompras.setModel(mtc);
    }
    public JLabel getAviso()
    {
        return this.lbAviso;
    }
    
    public Compra getCompra()
    {
        return seleeccionado;
    }
    
    private void getOptionPane()
    {
        if(op != null)
        {
            return;
        }
        Component pdr =this.getParent(); 
        while(pdr.getParent() != null)
        {
            if(pdr instanceof JOptionPane)
            {
                op = (JOptionPane)pdr;
                break;
            }            
            pdr = pdr.getParent();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private elaprendiz.gui.button.ButtonRect btnAnterior;
    private elaprendiz.gui.button.ButtonRect btnBuscar;
    private elaprendiz.gui.button.ButtonRect btnCerrar;
    private elaprendiz.gui.button.ButtonRect btnPrimero;
    private elaprendiz.gui.button.ButtonRect btnSeleccionar;
    private elaprendiz.gui.button.ButtonRect btnSiguiente;
    private elaprendiz.gui.button.ButtonRect btnUltimo;
    private com.toedter.calendar.JDateChooser fcAntes;
    private com.toedter.calendar.JDateChooser fcDespues;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private elaprendiz.gui.label.LabelRect lbAviso;
    private elaprendiz.gui.panel.PanelImage panelImage1;
    private javax.swing.JSpinner spItems;
    private javax.swing.JTable tblCompras;
    private elaprendiz.gui.textField.TextField txtNroDocumento;
    private javax.swing.JFormattedTextField txtPrecioMayor;
    private javax.swing.JFormattedTextField txtPrecioMenor;
    // End of variables declaration//GEN-END:variables
}
